
YAHOO.namespace("loading.container");

function initLoadingContainer() {

}

//var xmlcities;
	function fillRegions(http_request) {
//        Modalbox.hide();
        
		var xmlDocument = http_request.responseXML;
        
        $('region').style.display="";
        $('region').disabled=false;
        
/*
alert(
http_request.responseText
);
*/
		//put returned XML in the textarea
//		$('result').value = http_request.responseText;
		var xmlregions = xmlDocument.getElementsByTagName('region');
        
        
        $('region').options.length = 0;
        $('region').options[0] = new Option(
            "select a region",
            ""
        );
        $('region').options[0].disabled = true;
        $('region').options[1] = new Option(
            "------------------",
            ""
        );
        $('region').options[1].disabled = true;
        for(i=0; xmlregions[i]; i++ ) {
            $('region').options[2+i] = new Option(
                xmlregions[i].attributes.getNamedItem("name").value,
                xmlregions[i].attributes.getNamedItem("fips_region").value
            );
        }
   }
   
   function fillCountries(http_request) {
		var xmlDocument = http_request.responseXML;

        $(country).style.display="";

		//put returned XML in the textarea

		var xmlcountries = xmlDocument.getElementsByTagName('country');

        $(country).options.length = 0;
        for(i=0; xmlcountries[i]; i++ ) {
            $(country).options[i] = new Option(
                xmlcountries[i].attributes.getNamedItem("name").value,
                xmlcountries[i].attributes.getNamedItem("iso2").value
            );
        }
   }
   
/*    function fillCities(http_request) {
		var xmlDocument = http_request.responseXML;

        
        $('cityfield').style.display="";
        $('citylist').style.display="";
      
        
        
        xmlcities = xmlDocument.getElementsByTagName('city');
        
        var resultArray = new Array();
        
        for(i=0; xmlcities[i]; i++ ) {
            
            resultArray[i] = xmlcities[i].attributes.getNamedItem("accentcity").value;
        }
                
        new Autocompleter.Local('cityfield', 'citylist', resultArray, {});
        
   }*/
   
   function fillCities(http_request) {
		
		
		var xmlDocument = http_request.responseXML;

        
        //$('citycode').style.display="";
        var xmlcities = xmlDocument.getElementsByTagName('city');
        
        $('citycode').options.length = 0;
        $('citycode').options[0] = new Option(
            "select a city",
            ""
        );
        $('citycode').options[0].disabled = true;
        $('citycode').options[1] = new Option(
            "------------------",
            ""
        );
        $('citycode').options[1].disabled = true;
        for(i=0; xmlcities[i]; i++ ) {
            $('citycode').options[2+i] = new Option(
                xmlcities[i].attributes.getNamedItem("accentcity").value,
                xmlcities[i].attributes.getNamedItem("id").value
            );
        }
        
		YAHOO.loading.container.wait.hide();

//        $('loading').style.zIndex=2;
//        $('loading').style.display="none";
        $('citycode').style.zIndex=1;
        $('citycode').style.display="";
   }
   
   function getcitycode()
   {
   
        var citydesc = $('cityfield').value;
        
        for(i=0; xmlcities[i]; i++)
        {
            if (xmlcities[i].attributes.getNamedItem("accentcity").value == citydesc)
            {
                $('citycode').value = xmlcities[i].attributes.getNamedItem("id").value;
                return;
            }
        }
         
   }
   
    function updateRegions() {            
//        Modalbox.show('<div class=\'warning\'><p>Loading, please wait!</p></div>', 
//            {
//                title: 'Sono il titolo!', 
//                width: 300
//            });		

		$('region').style.color="black";

		$('citycode').style.zIndex=2;
        $('citycode').style.display="none";

        var url = '/proxypage.aspx';
		var pars = "?command=gimmeregions" 
					+ "&country=" + $F('country') ;
		
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: fillRegions
			});		
   }

   function updateCities() {

    YAHOO.loading.container.wait =
        new YAHOO.widget.Panel("wait",
                                  { width:"240px",
                                    fixedcenter:true,
                                    close:false,
                                    draggable:false,
                                    modal:true,
                                    visible:false,
                                    effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5}
                                  }
    );

    YAHOO.loading.container.wait.setHeader("Loading, please wait...");
    YAHOO.loading.container.wait.setBody("<img src=\"http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif\"/>");
    YAHOO.loading.container.wait.render(document.body);

		YAHOO.loading.container.wait.show();

//        $('loading').style.zIndex=1;
//        $('loading').style.display="";

        $('citycode').style.zIndex=2;
        $('citycode').style.display="none";
        
		var url = '/proxypage.aspx';
		var pars = "?command=gimmecities" 
					+ "&country=" + $F('country') 
					+ "&region=" + $F('region') ;
		
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: fillCities
			});
			
		
   }
   
   function updateCountries() {

        alert(  $F('country')  );

		var url = '../proxypage.aspx';
		var pars = "?command=gimmecountries";
							
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: fillCountries
			});
   }

   function doesUserOrEmailAlreadyExist() {

    YAHOO.loading.container.wait =
        new YAHOO.widget.Panel("wait",
                                  { width:"240px",
                                    fixedcenter:true,
                                    close:true,
                                    draggable:false,
                                    modal:true,
                                    visible:false,
                                    effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5}
                                  }
    );

    YAHOO.loading.container.wait.setHeader("Please wait...");
    YAHOO.loading.container.wait.setBody("Checking username and email.<br/><img src=\"http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif\"/>");
    YAHOO.loading.container.wait.render(document.body);

		YAHOO.loading.container.wait.show();
        
		var url = '/proxypage.aspx';
		var pars = "?command=userexistence" 
					+ "&username=" + $F('username') 
					+ "&language=IT" 
					+ "&email=" + $F('email') ;
		
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: doesUserOrEmailAlreadyExistAns
			});
			
		
   }

   function doesUserOrEmailAlreadyExistAns(http_request) {
				
		var xmlDocument = http_request.responseXML;
        
//		YAHOO.loading.container.wait.hide();

//        alert(http_request.responseText);

        var xmlerror = xmlDocument.getElementsByTagName('user')[0].attributes.getNamedItem("error").value;

//        alert("error:"+xmlerror);
        
        if( xmlerror == 0 )
            document.formRegistration.submit();
        else{
            YAHOO.loading.container.wait.setHeader("Attenzione...");
            YAHOO.loading.container.wait.setBody(xmlDocument.getElementsByTagName('user')[0].attributes.getNamedItem("message").value);
//            alert(xmlDocument.getElementsByTagName('user')[0].attributes.getNamedItem("message").value);
        }

   }
